<template>
	<view class="login-page">
		<u-navbar title="" :placeholder="true" bgColor="rgba(0,0,0,0.0)" :autoBack="true">
		</u-navbar>
		<view class="head-top">
			注册
		</view>
		<view class="content">
			<view class="title">
				账号/手机号
			</view>
			<view class="entry">
				<u--input placeholder="请输入手机号" border="none" v-model="tell"></u--input>
			</view>
			<view class="title">
				验证码
			</view>
			<view class="entry">
				<u-input type="Number" placeholder="请输入验证码" border="none" v-model="code">
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" startText="发送" seconds="60"
							changeText="X秒重新获取"></u-code>
						<u-button shape="circle" @tap="getCode" :text="tips" type="success" size="mini"
							color="#5755F2"></u-button>
					</template>
				</u-input>
			</view>
			<view class="title">
				密码
			</view>
			<view class="entry">
				<u-input :type="entry_type" placeholder="请输入密码" border="none" v-model="password">
					<template slot="suffix">
						<u-icon v-if="status == true" name="eye" color="#8B8DA0" size="24" @click="cutType"></u-icon>
						<u-icon v-else name="eye-off" color="#8B8DA0" size="24" @click="cutType"></u-icon>
					</template>
				</u-input>
			</view>
			<view class="title">
				邀请码
			</view>
			<view class="entry">
				<u--input type="text" placeholder="请输入邀请码(选填)" border="none" v-model="invite"></u--input>
			</view>
			<view class="login-btn" @click="goregister">
				<image src="https://m.lqvvvv.com/assets/static/login/register.png" mode=""></image>
			</view>
			<view class="agreement">
				<u-checkbox-group>
					<u-checkbox @change="handleChange" :value="checked" size="14" active-color="#19A9FE"
						shape="square"></u-checkbox>
				</u-checkbox-group>
				<view class="agreement-text">
					我已阅读并同意<text @click="goagreement('registrer_agreement')">《服务协议》</text>和 <text @click="goagreement('privacy_agreement')">《隐私协议》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tell: '',
				password: '',
				entry_type: 'password',
				status: false,
				checked: false,
				tips: '',
				code: '',
				invite: ''
			}
		},
		methods: {
			goagreement(name){
				this.$navTo.togo('/pages/my/agreement/index',{name:name})
			},
			goregister() {
				if (this.checked == false) {
					uni.showToast({
						title: '请仔细阅读并同意《注册协议》和《隐私协议》',
						icon: 'none'
					})
					return
				}
				if (this.tell == '') {
					uni.showToast({
						title: '手机号不能为空！',
						icon: 'none'
					})
					return
				}
				if (this.code == '') {
					uni.showToast({
						title: '验证码不能为空！',
						icon: 'none'
					})
					return
				}
				if (this.password == '') {
					uni.showToast({
						title: '密码不能为空！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask:true
				})
				let params = {
					mobile: this.tell,
					password: this.password,
					code:this.code,
					parent_code:this.invite
				}
				this.$Request({
					method: 'POST',
					url: '/api/user/register',
					data: params,
					ismsg: true,
				}).then(res => {
					console.log(res)
					uni.hideLoading();
					if (res.code == 1) {
						uni.showToast({
							title:"注册成功！",
							icon:'none'
						})
						setTimeout(()=>{
							this.$navTo.togo('/pages/login/index')
						},1000)
					}
				})
			},
			cutType() {
				if (this.status == false) {
					this.entry_type = 'text'
					this.status = true
				} else {
					this.entry_type = 'password'
					this.status = false
				}
			},
			handleChange() {
				this.checked = !this.checked
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if(this.tell == ''){
					uni.showToast({
						title:'手机号不能为空！',
						icon:'none'
					})
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					let params = {
						mobile: this.tell,
						event: 'register'
					}
					this.$Request({
						method: 'POST',
						url: '/api/sms/send',
						data: params,
						ismsg: true,
					}).then(res => {
						console.log(res)
						if (res.code == 1) {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login-page {
		min-height: 100vh;
		background-image: url('https://m.lqvvvv.com/assets/static/index/home-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 900rpx;
		padding: 0 30rpx 100rpx 30rpx;
		background-color: #F7F8FC;

		.head-top {
			padding: 30rpx;
			color: #19394A;
			font-weight: bold;
			font-size: 64rpx;
		}

		.content {
			background-image: url('https://m.lqvvvv.com/assets/static/login/login-bg.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			margin-top: 60rpx;
			padding: 50rpx;

			.title {
				margin-bottom: 30rpx;
				color: #2E314A;
				font-size: 32rpx;
				font-weight: bold;
			}

			.entry {
				background-color: #F9F9F9;
				padding: 20rpx 30rpx;
				border-radius: 20rpx;
				margin-bottom: 30rpx;
			}

			.login-btn {
				margin-top: 50rpx;

				image {
					width: 594rpx;
					height: 120rpx;
				}
			}

			.agreement {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 20rpx;
				.agreement-text {
					color: #8B8DA0;
					font-size: 24rpx;

					text {
						color: #5755F2;
					}
				}
			}

		}
	}
</style>